package ar.com.ford.it.fullEconomics.interfaces.threadExecutor;

import ar.com.ford.it.fullEconomics.interfaces.CommonInterfaceLauncher;
import ar.com.ford.it.fullEconomics.interfaces.interfaz.InterfaceProccesFile;
import ar.com.ford.it.fullEconomics.interfaces.strategys.UsageStrategy;
import commonj.timers.Timer;
import commonj.timers.TimerListener;


public class TimerInterfaceBMRunchee implements TimerListener {

	public String toString()
	{
		return this.getProcessName();
	}
	
	private String getProcessName()
	{
		return "TimerInterfaceBMRunchee";
	}
	
	public void timerExpired(Timer arg0) {

		System.out.println("+++ Ejecutando: " + this.toString() + " desde TimerManager");	
		
		InterfaceProccesFile inStrategy = (InterfaceProccesFile) new UsageStrategy();
		CommonInterfaceLauncher launcher = new CommonInterfaceLauncher();  
		launcher.processPendingInterfaceFiles(2, inStrategy, this.getProcessName(), "p_feefmc","T");
		
		/*
 	    LogProcessLogic logProcess = new LogProcessLogic();
	    String userId = "p_feefmc";
	    ReadFile parser;
	    Teesx03Intflog intflog = null;
	    Teesx02Fileconten fileConten = null;
	    Boolean bNewLogOpen = false;
	        
//	  Timestamp generationDate = new Timestamp(0);
        Teesx01Intfsource intfSource = new Teesx01Intfsource();
        intfSource.setEesx01IntfIdNumberK(2);
	    
    	try {                
            
            // --- TEST LSPARACI ---
            
            Collection<Teesx02Fileconten> pendFiles = null;
            
            pendFiles = logProcess.getFileContentsByStatus("P", intfSource, null, null);
            
            for (Iterator iterFiles = pendFiles.iterator(); iterFiles.hasNext();) {
        
            	fileConten = (Teesx02Fileconten) iterFiles.next();
            	
            		
            		if (fileConten.getEesx02FconFileBlobO() != null) {
		                parser = new ReadFile(fileConten,this.getProcessName());
		            
			            InterfaceProccesFile inStrategy = (InterfaceProccesFile) new UsageStrategy();
			            Teesx03Intflog lastIntfLog = logProcess.getLastIntfLog(fileConten
			                    .getEesx01Intfsource());
			            if (lastIntfLog != null
			                    && lastIntfLog.getEesx03IlogProcStatusCodeC()
			                            .equalsIgnoreCase("R")) {
			                long diffInHours = FullEconomicsDateUtil.getDifferenceInHours(
			                        lastIntfLog.getEesx03IlogStartM(), new Timestamp(System
			                                .currentTimeMillis()));
			                // si es mayor a 6 horas, lo cierro y abro el nuevo
			                if (diffInHours > 6) {
			                    logProcess
			                            .insertLogIntfLevent(
			                                    userId,
			                                    lastIntfLog,
			                                    "E",
			                                    "Se cancelo el proceso, tiempo de corrida mayor a 6 horas",
			                                    0);
			                    logProcess.updateLogInfo(lastIntfLog, "C", new Timestamp(
			                            System.currentTimeMillis()));
			                } // diffInHours > 6
			                else{
			                	logProcess
	                            .insertLogIntfLevent(
	                                    userId,
	                                    lastIntfLog,
	                                    "W",
	                                    "Hubo un intento de volver a procesar la ejecucion en curso",
	                                    0);
			                } // else diffInHours > 6
			                
			            }  // if lastIntfLog with status "R"unning  
			            else if (lastIntfLog != null
			                    && lastIntfLog.getEesx03IlogProcStatusCodeC()
	                            .equalsIgnoreCase("C")) {
			            	
			            	// Si el ultimo log del archivo dio Cancelado,
			            	// requiere intervencion para reproceso manual y
			            	// no continua
			            	logProcess
	                        .insertLogIntfLevent(
	                                userId,
	                                lastIntfLog,
	                                "E",
	                                "Ultimo proceso con Error. No se puede continuar proceso automatico hasta intervencion manual",
	                                0);
			            	return;
			      
			            } // if lastIntfLog with status "C"ancelled 
			            else {
			            	// Si el ultimo archivo dio "S" 
			            	// o no hay registros de ejecucion para este archivo
			            	
			                // Pone Status Proces Runin y ejecuta
			                intflog = logProcess.insertLogInf(intfSource, fileConten, userId, "R",
			                        new Timestamp(System.currentTimeMillis()));
			                
			                bNewLogOpen = true;
			                
			                parser.processLineByLine(inStrategy, intflog, userId);
			                logProcess.updateLogInfo(intflog, "S", new Timestamp(System
			                        .currentTimeMillis()));
			                
			                bNewLogOpen = false;
			       
			            } // else process Pending files
            		} // if file content is null
            		else {
            			intflog = logProcess.insertLogInf(intfSource, fileConten, userId, "R",
		                        new Timestamp(System.currentTimeMillis()));
            			
            			bNewLogOpen = true;
		             
		                logProcess
		                .insertLogIntfLevent(
		                        userId,
		                        intflog,
		                        "E",
		                        "No se encuentra el archivo para ser procesado",
		                        0);	
		                
		                logProcess.updateLogInfo(intflog, "C", new Timestamp(System
	    	                    .currentTimeMillis()));
		                
		                bNewLogOpen = false;
		                
		                return;
            		}
                 	
            } // for Iterator
            
    	} catch (FileNotFoundInDatabaseException fnf) {
       		System.out.println(fnf.getMessage());
       		
       		try {
       			if (fileConten != null) {
       			
       				if (!bNewLogOpen) {
       					intflog = logProcess.insertLogInf(intfSource, fileConten, userId, "R",
       	                        new Timestamp(System.currentTimeMillis()));
       	           	    bNewLogOpen = true;
       				}
       				
       				logProcess
                    .insertLogIntfLevent(
                            userId,
                            intflog,
                            "E",
                            "No se encuentra el archivo para ser procesado",
                            0);
       			}           	    
                
       		} catch(FullEconomicsException e) {
       		    e.printStackTrace();
       		}
    	} catch (FullEconomicsException e) {
            logProcess.updateLogInfo(intflog, "C", new Timestamp(System
                    .currentTimeMillis()));
            bNewLogOpen = false;
       		
        } // catch
    	finally {
    		if (bNewLogOpen) {
    		 logProcess.updateLogInfo(intflog, "C", new Timestamp(System
                    .currentTimeMillis()));
    		 bNewLogOpen = false;
    		}
    	}*/
	    System.out.println("+++ finalizando: " + this.toString() + " desde TimerManager");  
	}
	
}
